/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "ahkpserver.h"
#include "ahkpclient.h"
#include <QtDebug>
#include <QSettings>
#include <iostream>
#include "qhkp.h"

AHkpServer::AHkpServer(QHostAddress host,quint16 port,QObject *parent)
 : QTcpServer(parent),
 m_hkp(NULL)
{
   QSettings settings("../../gss.conf",QSettings::IniFormat);
   if(host == QHostAddress::Null)
    host = settings.value("hosts/HKP_IP").toString();
   if(port==0)
    port = settings.value("hosts/HKP_Port").toInt();
   qDebug()<<"listening on"<<host<<":"<<port;
   //listen(host,port);
   
   m_hkp = new QHkp("/dev/hkp0");
   
    if(m_hkp)
    {
        std::cout<<"hackerport openned."<<std::endl;
        if(m_hkp->isOpen())
        {
            m_hkp->ownInputPort(QHkp::A);
            m_hkp->ownInputPort(QHkp::B);
            m_hkp->ownOutputPort(QHkp::A);
            m_hkp->ownOutputPort(QHkp::B);
            m_hkp->ownInputPort(QHkp::analogAll);
            this->listen(QHostAddress(host),port);
            m_hkp->clearOutPort(QHkp::A);
            m_hkp->clearOutPort(QHkp::B);
            
        }
    }
    else std::cout<<"could not open hackerport."<<std::endl;
}


AHkpServer::~AHkpServer()
{
}




/*!
    \fn AHkpServer::incomingConnection(int socketId)
 */
void AHkpServer::incomingConnection(int socketId)
{
    qDebug()<<"connection established";
    AHkpClient *hkpClient = new AHkpClient(m_hkp,this);
    hkpClient->setSocketDescriptor(socketId);
}
